package com.AC.ACGBit;



import java.util.Scanner;

public class test1 {

    public static int mostLength(String str, char a, char b){
        str =str.toLowerCase();
        str =str.trim();
        if(str.indexOf(a) == -1 || str.indexOf(b) == -1)
            return -1;
        int len = str.length();
        int index_a = 0;
        int index_b = 0;
        int res1 = 0, res2 = 0;
        for(int i = 0; i<len; i++){
            if(str.charAt(i) == a){
                index_a = i;
                break;
            }
        }
        for(int i = len-1; i>=0; i--){
            if(str.charAt(i) == b){
                index_b = i;
                break;
            }
        }

        res1 = Math.abs(index_a - index_b);

        for(int i = 0; i<len; i++){
            if(str.charAt(i) == b){
                index_b = i;
                break;
            }
        }
        for(int i = len-1; i>=0; i--){
            if(str.charAt(i) == a){
                index_a = i;
                break;
            }
        }

        res2 = Math.abs(index_a - index_b);

        return Math.max(res1,res2);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        String c1 = sc.next();
        String c2 = sc.next();
        sc.close();
        System.out.println(mostLength(s, c1.toLowerCase().toCharArray()[0], c2.toLowerCase().toCharArray()[0]));
    }

}
